package rec;

/**
 * 一、 递归
 * 5. Pow(x, n)
 * 2024-11-4
 */
public class demo5 {
    public double myPow(double x, int n) {
        return n < 0 ? 1.0 / pow(x, n) : pow(x, n);
    }

    public double pow(double x, int n){
        if(n == 0) return 1.0;

        double tmp = pow(x, n / 2);
        return n % 2 == 0 ? tmp * tmp : tmp * tmp * x;
    }
}
